package com.example.dairenkun.neufoodshop.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.dairenkun.neufoodshop.R;
import com.example.dairenkun.neufoodshop.bean.Buy;
import com.example.dairenkun.neufoodshop.bean.User;
import com.example.dairenkun.neufoodshop.model.OrderModel;
import com.example.dairenkun.neufoodshop.model.UserModel;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.Date;

import retrofit2.Call;
import retrofit2.Callback;

public class BuyActivity extends AppCompatActivity {
          private ImageView foodImage;
          private TextView foodName;
          private TextView foodPrice;
          private ImageButton numReduce;
          private ImageButton numAdd;
          private EditText foodNum;
          private TextView userName;
          private EditText Address;
          private  EditText suggestTime;
          private  TextView TotalPrice;
          private Button btn_confirm;
    String name;
    int userid;
    int foodid;
    int num=1;//初始数量
    int money;//食品单价
    double totalmoney;//总价
    int sum;//总数
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_buy);
        initViews();
        getData();
        initEvents();
        initDatas();
    }
    private void  initViews(){
        foodImage=(ImageView)findViewById(R.id.buy_iamge);
        foodName=(TextView)findViewById(R.id.buy_foodname);
        foodPrice=(TextView)findViewById(R.id.buy_food_price);
        numReduce=(ImageButton)findViewById(R.id.imgBtn_down);
        numAdd=(ImageButton)findViewById(R.id.imgBtn_up);
        foodNum=(EditText) findViewById(R.id.buy_foodNum);
        userName=(TextView)findViewById(R.id.buy_username);
        Address=(EditText)findViewById(R.id.buy_address);
        suggestTime=(EditText)findViewById(R.id.buy_time);
        TotalPrice=(TextView)findViewById(R.id.buy_price);
        btn_confirm=(Button)findViewById(R.id.btn_buy_confirm);

    }
    private void initDatas(){
        //获取网上的数据/
        UserModel userModel=new UserModel();
        Call call=userModel.UserInfo(userid);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, retrofit2.Response<User > response)
            {
                final  User user=response.body();
              Address.setText(user.getAddress());
            }
            @Override
            public void onFailure(Call<User> call, Throwable t) {

            }
        });
    }
    private  void initEvents(){
        numAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String n=foodNum.getText().toString();
                foodNum.setText(String.valueOf(Integer.parseInt(n)+1));
                int sum=   Integer.valueOf(foodNum.getText().toString());
                totalmoney=money*sum;
                TotalPrice.setText("¥"+String.valueOf(totalmoney));
            }
        });
        numReduce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String n=foodNum.getText().toString();
                int nn=Integer.parseInt(n)-1;
                foodNum.setText(String.valueOf(nn<1?1:nn));
                sum=   Integer.valueOf(foodNum.getText().toString());
                totalmoney=money*sum;
                TotalPrice.setText(String.valueOf(totalmoney));
            }
        });
        btn_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //获取网上的数据/
                OrderModel orderModel=new OrderModel();
                Call call=orderModel.Buy(userid,foodid,sum,totalmoney ,suggestTime.getText().toString());
                call.enqueue(new Callback<Buy>() {
                    @Override
                    public void onResponse(Call<Buy> call, retrofit2.Response<Buy> response)
                    {
                        Buy buy=response.body();
                        String result=buy.getSuccess();
                        if("0".equals(result))
                        {//失败处理
                            Toast.makeText(getApplication(), "支付失败！", Toast.LENGTH_SHORT).show();
                        } else
                        {
                            Toast.makeText(getApplication(), "购买成功", Toast.LENGTH_SHORT).show();
                        }

                    }
                    @Override
                    public void onFailure(Call<Buy> call, Throwable t) {
                    }
                });
            }
        });
    }
    private  void getData(){
        getUser();
        Intent intent=getIntent();
        money=Integer.valueOf(intent.getStringExtra("price"));
        foodid = Integer.valueOf(intent.getStringExtra("food_id"));
        foodPrice.setText("¥"+intent.getStringExtra("price"));
        foodName.setText(intent.getStringExtra("foodname"));
        userName.setText(name);
        TotalPrice.setText("¥"+intent.getStringExtra("price"));

        if(intent.getStringExtra("pic").equals(""))
            Picasso.with(BuyActivity.this).load(R.mipmap.ic_launcher).into(foodImage);
        else Picasso.with(BuyActivity.this).load(intent.getStringExtra("pic")).into(foodImage);
      /*  SimpleDateFormat formatter = new SimpleDateFormat ("yyyy年MM月dd日 HH:mm:ss ");
        Date curDate = new Date(System.currentTimeMillis());//获取当前时间
        final String suggesttime = formatter.format(curDate);*/
        foodNum.setText(num+"");

    }
         private void getUser(){
                SharedPreferences sp = getApplication().getSharedPreferences("UserInfo", getApplication().MODE_PRIVATE);
                userid = Integer.valueOf(sp.getString("user_id", null));
               name=sp.getString("username",null);
            }
}
